package com.itheima.upload.rabbit;

import com.alibaba.fastjson2.JSON;
import com.itheima.entity.dto.UploadDto;
import com.itheima.upload.oss.OssProperties;
import com.itheima.upload.oss.OssTemplete;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author :wujunjie
 * createTime :2022-12-22 19:46
 * description :
 */
@Component
@Slf4j
public class MyListener {

    @Autowired
    private OssTemplete OssTemplete;

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue("ossqueue.direct"),
                    exchange = @Exchange("ossexchage.direct"),
                    key = {"osskey"}
            )
    )
    public void listener(String message){
        if (StringUtils.isEmpty(message)){
            throw new RuntimeException("信息是空的");
        }
        UploadDto uploadDto = JSON.parseObject(message, UploadDto.class);
        String s = OssTemplete.uploadForMq(uploadDto);
        log.info("图片上传成功：{}",s);
    }
}
